package com.huibei.wateradmin.authority.util;


import net.sourceforge.pinyin4j.PinyinHelper;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;


/**
 * @author Stryker
 * @date 2016年4月29日
 * @TODO 字符串工具类
 */
public class StringUtil
{

	/**
	 * 字符串为null 或 "" 返回true，否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNullOrEmpty(String str) {
		return (str == null || str.equals(""));
	}

	/**
	 * 字符串不为 null 或 "" 返回true
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotNullAndEmpty(String str) {
		return (str != null && (!str.equals("")));
	}

	/**
	 * 判定字符串是否为null,或只有空白字符空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNullOrTrimEmpty(String s) {
		if (s == null)
			return true;

		return s.trim().equals("");
	}

	public static String trim(final String str) {
		if (str == null) {
			return null;
		}
		return str.trim();
	}

	public static String trimToNull(String str) {
		if (str == null) {
			return null;
		}
		str = str.trim();
		if ("".equals(str)) {
			return null;
		}
		return str;
	}

	public static boolean isBlank(final String str) {
		return str == null || "".equals(str) || "".equals(str.trim());
	}

	public static String notNull(final String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	public static String nl2br(String note) {
		if (note == null)
			return null;
		return note.replaceAll("\n", "<br />");
	}

	/**
	 * Test if the given String starts with the specified prefix, ignoring upper/lower case.
	 * 
	 * @param str
	 *            the String to check
	 * @param prefix
	 *            the prefix to look for
	 * @see String#startsWith
	 */
	public static boolean startsWithIgnoreCase(String str, String prefix) {
		if (str == null || prefix == null) {
			return false;
		}
		if (str.startsWith(prefix)) {
			return true;
		}
		if (str.length() < prefix.length()) {
			return false;
		}
		String lcStr = str.substring(0, prefix.length()).toLowerCase();
		String lcPrefix = prefix.toLowerCase();
		return lcStr.equals(lcPrefix);
	}

	/**
	 * 精度
	 * 
	 * @param value
	 * @param decimal
	 * @return
	 */
	public static double doubleFormat(double value, int decimal) {
		value = Math.round(value * Math.pow(10, decimal)) / Math.pow(10, decimal);
		BigDecimal bd = new BigDecimal(value);
		bd = bd.setScale(decimal, BigDecimal.ROUND_HALF_UP);
		return bd.doubleValue();
	}

	public static String urlDecode(String s, String enc) {
		if (isNullOrEmpty(s))
			return s;

		try {
			return URLDecoder.decode(s, enc);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获得指定范围的随机值 >minNum <=maxNum
	 * 
	 * @param minNum
	 * @param maxNum
	 * @return
	 */
	public static int getRandom(int minNum, int maxNum) {
		return (int) ((maxNum - minNum) * Math.random() + minNum + 1);
	}

	/**
	 * 是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNum(String str) {
		if (isNullOrTrimEmpty(str))
			return false;

		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?)$");
	}

	/**
	 * 是否是整数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDigital(String str) {
		if (isNullOrTrimEmpty(str)) {
			return false;
		}
		return str.matches("^[-+]?[1-9][0-9]*$");
	}

	public static boolean isMobilePhone(String s) {
		if (isNullOrEmpty(s)) {
			return false;
		}
		return s.matches("^[1][3-8][0-9]{9}$");
	}

	/**
	 * 字符串转asc码
	 * 
	 * @param str
	 * @return
	 */
	public static String strToAscii(String str) {
		StringBuffer sb = new StringBuffer();
		char[] chars = str.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			sb.append((int) chars[i]);
		}
		return sb.toString();
	}

	public static boolean CheckBankCardNumber(String ccNumber) {
		int sum = 0;
		boolean alternate = false;
		for (int i = ccNumber.length() - 1; i >= 0; i--) {
			int n = Integer.parseInt(ccNumber.substring(i, i + 1));
			if (alternate) {
				n *= 2;
				if (n > 9) {
					n = (n % 10) + 1;
				}
			}
			sum += n;
			alternate = !alternate;
		}
		return (sum % 10 == 0);
	}

	/**
	 * 获得首字母拼音
	 * 
	 * @param value
	 * @return
	 */
	public static String getSpell(String value) {
		String convert = "";
		if (value.length() > 0) {
			char word = value.charAt(0);
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert = String.valueOf(pinyinArray[0].charAt(0)).toUpperCase();
			}
		}
		return convert;
	}

	/**
	 * @param upperCase
	 *            true 字母全大写 false 字母全小写
	 * @description 生成32位随机数，包括字母和数字，如6A7A20338689451D9E214683680F992C
	 */
	public static String getUUIDString(boolean upperCase) {
		String result = UUID.randomUUID().toString().replaceAll("-", "");
		if (upperCase)
			result = result.toUpperCase();

		return result;
	}

	/**
	 * @description 使用固定字符连接字符串
	 */
	public static String joinString(String seprator, String... strings) {
		if (strings.length == 0 || strings == null)
			return "";

		StringBuffer result = new StringBuffer();
		for (int i = 0; i < strings.length; i++) {
			String string = strings[i];
			result.append(string);
			if (i != (strings.length - 1))
				result.append(seprator);
		}
		return result.toString();
	}

	/**
	 * 根据两个分隔符将字符串切割成map
	 * 
	 * @param mapString
	 * @param separator
	 * @param innerSeparator
	 * @return
	 */
	public static Map<String, String> transStringToMap(String mapString, String separator, String innerSeparator) {
		Map<String, String> map = new HashMap<String, String>();
		StringTokenizer items;
		for (StringTokenizer entrys = new StringTokenizer(mapString, separator); entrys.hasMoreTokens(); map
				.put(items.nextToken(), items.hasMoreTokens() ? ((String) (items.nextToken())) : null)) {
			items = new StringTokenizer(entrys.nextToken(), innerSeparator);
		}
		return map;
	}

	/**
	 * @TODO 得到一个随机数字
	 *            字符串长度
	 * @String
	 */
	public static Integer getOneRandomNumber() {
		Random random = new Random();
		return Math.abs(random.nextInt() % 10);
	}

	/**
	 * @TODO 得到一个指定长度的纯数字字符串
	 * @param length
	 *            字符串长度
	 * @String
	 */
	public static String getRandomNumberSpecifiedLength(Integer length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append(getOneRandomNumber());
		}
		return sb.toString();
	}

}
